package cn.edu.zut.score.core.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author zby
 * @date 2021/06/24
 */
@JsonInclude(Include.NON_NULL)
@ApiModel("响应通用参数包装")
public class RestResponse<T> {
    public static final Integer SUCCESS = 0;

    @ApiModelProperty("响应错误编码,0为正常")
    private Integer code;

    @ApiModelProperty("响应错误信息")
    private String msg;

    @ApiModelProperty("响应内容")
    private T result;

    public boolean isSuccess() {
        return SUCCESS.equals(this.code);
    }

    public static <T> RestResponse<T> success() {
        return new RestResponse();
    }

    public static <T> RestResponse<T> success(T result) {
        RestResponse<T> response = new RestResponse();
        response.setResult(result);
        return response;
    }

    public static <T> RestResponse<T> success(String msg, T result) {
        RestResponse<T> response = new RestResponse();
        response.setResult(result);
        response.setCode(200);
        response.setMsg(msg);
        return response;
    }

    public static <T> RestResponse<T> success(Integer code, String msg, T result) {
        RestResponse<T> response = new RestResponse();
        response.setResult(result);
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    /**
     * 返回失败信息
     *
     * @param msg 失败信息
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> fail(String msg) {
        RestResponse<T> response = new RestResponse();
        response.setCode(-2);
        if (msg == null) {
            //传来的msg可能为null，就没有了。所以提前处理
            msg = "操作失败，请稍后重试";
        }
        response.setMsg(msg);
        return response;
    }

    /**
     * 返回失败信息，指定错误编码
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> fail(Integer code, String msg) {
        RestResponse<T> response = new RestResponse();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    public RestResponse() {
        this(0, "success", null);
    }

    public RestResponse(Integer code, String msg) {
        this(code, msg, null);
    }

    public RestResponse(Integer code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getResult() {
        return this.result;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof RestResponse)) {
            return false;
        } else {
            RestResponse<?> other = (RestResponse) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47:
                {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    /*
                    别改成if(this$code == null && other$code == null || this$code.equals(other$code)) break label47:
                    否则无法保证this$code非空，可能报错
                     */
                    if (this$code == null) {
                        if (other$code == null) {
                            break label47;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                Object this$result = this.getResult();
                Object other$result = other.getResult();
                if (this$result == null) {
                    if (other$result != null) {
                        return false;
                    }
                } else if (!this$result.equals(other$result)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof RestResponse;
    }

    @Override
    public int hashCode() {
        int result = 1;
        Object $code = this.getCode();
        result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null ? 43 : $msg.hashCode());
        Object $result = this.getResult();
        result = result * 59 + ($result == null ? 43 : $result.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "RestResponse(code=" + this.getCode() + ", msg=" + this.getMsg() + ", result=" + this.getResult() + ")";
    }
}
